package com.ruoyi.web.controller.task;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.YcTask;
import com.ruoyi.domain.YcTaskScore;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IYcTaskScoreService;
import com.ruoyi.service.IYcTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 课题Controller
 *
 * @author ruoyi
 * @date 2020-09-08
 */
@Controller
@RequestMapping("/ruoyi/task")
public class YcTaskController extends BaseController {
    private String prefix = "ruoyi/task";

    private final static String ALL="全部";

    @Autowired
    private IYcTaskService ycTaskService;

    @Resource
    private IYcTaskScoreService ycTaskScoreService;


    @RequiresPermissions("ruoyi:task:view")
    @GetMapping()
    public String task() {
        return prefix + "/task";
    }

    /**
     * 查询课题列表
     */
    @RequiresPermissions("ruoyi:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YcTask ycTask) {
        startPage();
        //ALL代表全部,如果搜索框内是全部就给传一个空值
        if (ALL.equals(ycTask.getProjectLevel())){
            ycTask.setProjectLevel(null);
            ycTask.setUserId(ShiroUtils.getUserId());
            List<YcTask> list = ycTaskService.selectYcTaskList(ycTask);
            return getDataTable(list);
        }
        ycTask.setUserId(ShiroUtils.getUserId());
        List<YcTask> list = ycTaskService.selectYcTaskList(ycTask);
        return getDataTable(list);
    }

    /**
     * 导出课题列表
     */
    @RequiresPermissions("ruoyi:task:export")
    @Log(title = "课题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YcTask ycTask) {
        List<YcTask> list = ycTaskService.selectYcTaskList(ycTask);
        ExcelUtil<YcTask> util = new ExcelUtil<YcTask>(YcTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 新增课题
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存课题
     */
    @RequiresPermissions("ruoyi:task:add")
    @Log(title = "课题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YcTask ycTask) {
        //获取用户输入的项目级别  (ycTask.getProjectLevel();)
        //获取到该文件是属于什么等级的,通过等级查询出该等级的信息,并且获取到该等级的奖励,然后使用set方法赋值给对象
        ycTask.setAward(ycTaskScoreService.selectYcTaskScoreByLevel(ycTask.getProjectLevel()).getAward());
        //获取到该文件是属于什么等级的,通过等级查询出该等级的信息,并且获取到该等级的赋分,然后使用set方法赋值给对象
        ycTask.setScore(ycTaskScoreService.selectYcTaskScoreByLevel(ycTask.getProjectLevel()).getScore());
        //添加上传人
        ycTask.setProposer(ShiroUtils.getLoginName());
        //添加用户id
        ycTask.setUserId(ShiroUtils.getUserId());
        return toAjax(ycTaskService.insertYcTask(ycTask));
    }

    /**
     * 修改课题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        YcTask ycTask = ycTaskService.selectYcTaskById(id);
        mmap.put("ycTask", ycTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存课题
     */
    @RequiresPermissions("ruoyi:task:edit")
    @Log(title = "课题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YcTask ycTask) {
        //获取到该文件是属于什么等级的,通过等级查询出该等级的信息,并且获取到该等级的奖励,然后使用set方法赋值给对象
        ycTask.setAward(ycTaskScoreService.selectYcTaskScoreByLevel(ycTask.getProjectLevel()).getAward());
        //获取到该文件是属于什么等级的,通过等级查询出该等级的信息,并且获取到该等级的赋分,然后使用set方法赋值给对象
        ycTask.setScore(ycTaskScoreService.selectYcTaskScoreByLevel(ycTask.getProjectLevel()).getScore());
        return toAjax(ycTaskService.updateYcTask(ycTask));
    }

    /**
     * 删除课题
     */
    @RequiresPermissions("ruoyi:task:remove")
    @Log(title = "课题", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ycTaskService.deleteYcTaskByIds(ids));
    }


    /**
     * 查询课题字典表
     * @param ycTaskScore
     * @return
     */
    @GetMapping("/score")
    @ResponseBody
    public List<YcTaskScore> plans(YcTaskScore ycTaskScore) {
        //查询课题字典表所有的属性
        List<YcTaskScore> ycTaskScores = ycTaskScoreService.selectYcTaskScoreList(ycTaskScore);
        return ycTaskScores;

    }

    /**
     * 提交申请备案
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        //更新申报状态为待备案
        ycTaskService.submitApplications(id);
        return success();
    }


    /**
     * 提交申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitForApproval")
    @ResponseBody
    public synchronized AjaxResult submitForApproval(Long id) {
        //更新审批状态为待审批
        ycTaskService.updateSubmitForApproval(id);
        return success();
    }

    /**
     * 撤销申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocation")
    @ResponseBody
    public AjaxResult revocation(Long id) {
        //更新审批状态为未备案
        ycTaskService.withdrawClaim(id);
        return success();
    }


}
